package gongshw.comic;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.DocumentException;
import org.dom4j.Element;

import android.util.Log;

import gongshw.comic.conan.C;
import gongshw.comic.web.HtmlGetter;
import gongshw.xml.XmlHandler;

public class IndexGetter {
	static private String name[] ;
	static private String url[];
	static private String lastChapter[];
	
	public static void requsetComicIndex(String indexUrl){
		List<String> nl = new ArrayList<String>();
		List<String> ul = new ArrayList<String>();
		List<String> ll = new ArrayList<String>();
		
		String html = HtmlGetter.getHotComicFromUrl(indexUrl);
		String csa[] = html.split("(<dl>)|(</dl>)");
		//System.out.println(csa.length);
		int i = 0;
		for(String s:csa){
			String xml = "<comic>"+s+"</comic>";
			try {
				XmlHandler xh = new XmlHandler(xml);
				Log.v("requsetComicIndex", xml);
				Element[] ele = xh.getElementArray("a");
				if(ele.length!=2)
					continue;
				i++;
				if(i>C.hotNumber)
					break;
				String urlTail = ele[0].attributeValue("href");
				String name = ele[0].getText();
				String lastChapter = getLastChapterTittle(xml);
				System.out.println(urlTail+" "+name+" "+lastChapter);
				nl.add(name);
				ul.add(urlTail);
				ll.add(lastChapter);
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		name = new String[C.hotNumber];
		url = new String[C.hotNumber];
		lastChapter = new String[C.hotNumber];
		
		nl.toArray(name);
		ul.toArray(url);
		ll.toArray(lastChapter);
	}
	
	private static String getLastChapterTittle(String xml){
		String[] sa = xml.split("Title=\"");
		return sa[1].split("\"")[0];
	}
	
	public static String[] getComicIndexName(){
		return name;
	}
	
	public static String[] getComicIndexUrl(){
		return url;
	}
	
	public static String[] getLatChapterList(){
		return lastChapter;
	}
}